package com.pro.controller;

import com.google.gson.Gson;
import com.pro.pojo.JsDept;
import com.pro.pojo.JsDeptAuthority;
import com.pro.pojo.JsonData;
import com.pro.service.JsDeptListService;
import com.pro.utils.RedisCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * ClassName:JsDeptListController
 * Package:com.pro.controller
 * Description:
 *
 * @Date:2021/11/14 20:57
 * @Author: XJR
 */
@RestController
@RequestMapping("/DeptList")
public class JsDeptListController {
    @Autowired
    private JsDeptListService service;

    @Autowired
    private RedisCache redisCache;

    private Gson gson = new Gson();

    //查看部门信息
    @RequestMapping("/selectJsDeptList")
    public JsonData selectJsDeptList(String deptName, String trueName, String createDate, String status){
        deptName = !("").equals(deptName) && deptName !=null?"%"+deptName+"%":"%%";
        trueName = !("").equals(trueName) && trueName !=null?"%"+trueName+"%":"%%";
        createDate = !("").equals(createDate) && createDate !=null?"%"+createDate+"%":"%%";
        status = !("").equals(status) && status !=null?"%"+status+"%":"%%";
        List<JsDept> data = service.selectJsDeptList(deptName, trueName, createDate, status);
        Integer count = service.countJsDeptList(deptName, trueName, createDate, status);
        //redis缓存
        String deptListSelectJsDeptList = redisCache.getCacheObject("DeptListSelectJsDeptList");
        List list = gson.fromJson(deptListSelectJsDeptList, List.class);
        Integer integer = redisCache.getCacheObject("DeptListCountJsDeptList");
        return list!=null? JsonData.buildSuccess(list,integer): JsonData.buildError("服务端错误");
    };

    //修改状态
    @RequestMapping("/updateJsDeptListStatus")
    public JsonData updateJsDeptListStatus(JsDept jsDept){
        Boolean aBoolean = service.updateJsDeptListStatus(jsDept);
        System.out.println(jsDept);
        return aBoolean!=false? JsonData.buildSuccess(aBoolean): JsonData.buildError("服务端错误");
    };

    //编辑
        //通过id查询
        @RequestMapping("/selectJsDept")
        public JsonData selectJsDept(Integer id){
            JsDept jsDept = service.selectJsDept(id);
            return jsDept!=null? JsonData.buildSuccess(jsDept): JsonData.buildError("服务端错误");
        };
        //编辑
        @RequestMapping("/updateJsDept")
        public JsonData updateJsDept(JsDept jsDept){
            Boolean aBoolean = service.updateJsDept(jsDept);
            return aBoolean!=false? JsonData.buildSuccess(aBoolean): JsonData.buildError("服务端错误");
        };

    //删除
    @RequestMapping("/delJsDeptList")
    public JsonData delJsDeptList(Integer id){
        Boolean aBoolean = service.delJsDeptList(id);
        return aBoolean!=false? JsonData.buildSuccess(aBoolean): JsonData.buildError("服务端错误");
    };


    //添加部门信息
    @RequestMapping("/addJsDeptList")
    public JsonData addJsDeptList(JsDept jsDept){
        Boolean aBoolean = service.addJsDeptList(jsDept);
        Boolean aBoolean1 = service.addJsDeptAuthority(new JsDeptAuthority(jsDept.getId(), jsDept.getDeptDescirbe()));
        Boolean b = false;
        if(aBoolean&&aBoolean1!=null){
            b = true;
        }
        return b!=false? JsonData.buildSuccess(b): JsonData.buildError("服务端错误");
    };
}
